/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gr_graph_fun.h>
#include "GR_write_html.h"

FILE *
assert_open_html_file(const char *name_file)
{
  FILE *fp;
  if(!name_file)
    {
      fprintf(stderr, "HTML File name is missing\n");
      return NULL;
    }
  fp = fopen(name_file, "w+");
  if(!fp)
    {
      fprintf(stderr, "Can't open %s \n",name_file);
      return NULL;
    }
  return fp;
}

void
write_head_html_file(FILE *fp, const char *wtitle)
{
    if(wtitle)
	fprintf(fp,"<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n</HEAD>\n", wtitle);
    else
	fprintf(fp,"<HTML>\n<HEAD>\n</HEAD>\n");
}

void
write_style_html_file(FILE *fp)
{
    fprintf(fp,
   "<STYLE TYPE=\"text/css\">\nTD {font-size: 8pt; fontFamily:Helvetica;}");

    fprintf(fp,"</STYLE>");
}


void
write_end_html_file(FILE *fp)
{
  fprintf(fp,"</HTML>\n");
}

void
write_start_body_html_file(FILE *fp, const char *scolor)
{
    if(scolor)
	fprintf(fp, "<BODY bgcolor=\"%s\">\n", scolor);
    else
	fprintf(fp, "<BODY>\n");
}

void
write_header_html_file(FILE *fp, int num, const char *sheader)
{
    if(num > 0 && num < 6 && sheader)
	fprintf(fp, "<H%d>%s</H%d>\n", num,sheader,num);
}

void
write_bold_html_file(FILE *fp, const char *sbold)
{
    if(sbold)
	fprintf(fp, "<B>%s</B>", sbold);
}

void
write_break_html_file(FILE *fp)
{
    fprintf(fp,"<BR>\n");
}

void
write_blank_spaces_html_file(FILE *fp,int nspaces)
{	
  int i = 0;
  for(i=0;i<nspaces;i++)
      fprintf(fp,"&nbsp;");
}

void
write_horizontal_rule_html_file(FILE *fp)
{
    fprintf(fp,"<HR>\n");
}

void
write_start_ul_html_file(FILE *fp)
{
    fprintf(fp,"<UL>\n");	
}

void
write_end_ul_html_file(FILE *fp)
{
    fprintf(fp,"</UL>\n");	
}
void
write_start_ol_html_file(FILE *fp)
{
    fprintf(fp,"<OL>\n");	
}

void
write_end_ol_html_file(FILE *fp)
{
    fprintf(fp,"</OL>\n");	
}

void
write_item_html_file(FILE *fp)
{
    fprintf(fp,"<LI>\n");	
}

void 
write_end_body_html_file(FILE *fp)
{
   fprintf(fp,"</BODY>\n"); 
}

void
write_start_a_href_html_file(FILE *fp, const char *shref)
{
    if(shref)
	fprintf(fp,"<A HREF=\"%s\">",shref); 
}

void
write_end_a_href_html_file(FILE *fp)
{
    fprintf(fp,"</A>\n");
}


void
write_start_pre_html_file(FILE *fp)
{
    fprintf(fp,"<PRE>");	
}

void
write_end_pre_html_file(FILE *fp)
{
    fprintf(fp,"</PRE>\n");	
}


void
write_start_td_html_file(FILE *fp, int srows)
{
  if(srows ==0)
    fprintf(fp,"\n<TD>");
  else
    fprintf(fp,"\n<TD ROWSPAN=\"%d\">", srows);
}

void
write_start_td_html_file_cols(FILE *fp, int scols)
{
  if(scols ==0)
    fprintf(fp,"\n<TD>");
  else
    fprintf(fp,"\n<TD COLSPAN=\"%d\" VALIGN=\"TOP\">", scols);
}

void
write_start_td_html_file_rows_top(FILE *fp, int srows)
{
  if(srows ==0)
    fprintf(fp,"\n<TD>");
  else
    fprintf(fp,"\n<TD ROWSPAN=\"%d\" VALIGN=\"TOP\">", srows);
}

void
write_end_td_html_file(FILE *fp)
{
  fprintf(fp,"</TD>\n");
}

void
write_start_th_html_file(FILE *fp, int srows)
{
  if(srows ==0)
    fprintf(fp,"\n<TH ALIGN=\"LEFT\">");
  else
    fprintf(fp,"\n<TH ROWSPAN=\"%d\" ALIGN=\"LEFT\" >", srows);
}

void
write_end_th_html_file(FILE *fp)
{
  fprintf(fp,"</TH>\n");
}

void
write_start_tr_html_file(FILE *fp)
{
  fprintf(fp,"\n<TR>");
}

void
write_start_tr_html_file_color(FILE *fp,char *scolor)
{
    if(scolor)
	fprintf(fp,"\n<TR BGCOLOR=\"%s\">",scolor);
    else
	fprintf(fp,"\n<TR>");
}

void
write_end_tr_html_file(FILE *fp)
{
  fprintf(fp,"</TR>\n");
}

void
write_start_table_html_file(FILE *fp, int max_level)
{
  fprintf(fp,"\n<TABLE BORDER COLS=\"%d\" BGCOLOR=\"#FFFFCC\">", max_level);
}

void
write_start_simple_table_html_file(FILE *fp)
{
  fprintf(fp,"\n<TABLE BORDER BGCOLOR=\"#FFFFCC\">");
}

void
write_end_table_html_file(FILE *fp)
{
  fprintf(fp,"</TABLE>\n");
}

void
write_string_html_file(FILE *fp, const char *str)
{
  char *p = (char *)str;
  if(!p)
      return;
  while(*p!='\0')
    {
      if(*p == '<')
	fprintf(fp,"&lt;");
      else if(*p == '>')
	fprintf(fp,"&gt;");
      else if(*p == '&')
	fprintf(fp,"&amp;");
      else if(*p == '"')
	fprintf(fp,"&quot;");
      else
	fprintf(fp,"%c",*p);

      p++;
    }
}

void 
write_row_data(FILE *fp, const char *svalue, int data)
{
  write_start_tr_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,"%d",data);
  write_end_td_html_file(fp);
  write_end_tr_html_file(fp);
}

void 
write_row_data2(FILE *fp, const char *svalue, int data1, int data2)
{
  write_start_tr_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,"%d",data1);
  write_end_td_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,"%d",data2);
  write_end_td_html_file(fp);
  write_end_tr_html_file(fp);
}

void
write_td_data(FILE *fp, int data)
{
  write_start_td_html_file(fp,0);
  fprintf(fp,"%d",data);
  write_end_td_html_file(fp);
}

void
write_td_string(FILE *fp, const char *svalue)
{
  write_start_td_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
}


void
write_td_string_span_cols(FILE *fp, char *svalue,int spancols)
{
  write_start_td_html_file_cols(fp,spancols);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
}


void
write_td_string_span_rows_top(FILE *fp, char *svalue,int span)
{
  write_start_td_html_file_rows_top(fp,span);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
}

void
write_row_data5(FILE *fp, const char *svalue, int data1, int data2, 
		int data3, int data4, int data5)
{
  write_start_tr_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
  write_td_data(fp,data1);
  write_td_data(fp,data2);
  write_td_data(fp,data3);	
  write_td_data(fp,data4);	
  write_td_data(fp,data5);	
  write_end_tr_html_file(fp);
}

void
write_row_string3(FILE *fp, const char *svalue, char * value1, char * value2,
		  char * value3)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,(char *)svalue);
  write_td_string(fp,value1);
  write_td_string(fp,value2);
  write_td_string(fp,value3);	
  write_end_tr_html_file(fp);
}

void
write_row_string4(FILE *fp, const char *svalue, char * value1, char * value2,
		  char * value3,char *value4)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,(char *)svalue);
  write_td_string(fp,value1);
  write_td_string(fp,value2);
  write_td_string(fp,value3);	
  write_td_string(fp,value4);	
  write_end_tr_html_file(fp);
}


void
write_row_string5(FILE *fp, const char *svalue, const char * value1, 
		  const char * value2,
		  const char * value3,const char *value4,const char *value5)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,svalue);
  write_td_string(fp,value1);
  write_td_string(fp,value2);
  write_td_string(fp,value3);	
  write_td_string(fp,value4);	
  write_td_string(fp,value5);	
  write_end_tr_html_file(fp);
}

void 
write_row_string(FILE *fp, const char *svalue, const char *sdata)
{
  write_start_tr_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,sdata);
  write_end_td_html_file(fp);
  write_end_tr_html_file(fp);
}

void 
write_row_string2(FILE *fp, const char *svalue, const char *sdata1, 
		  const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_td_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,sdata1);
  write_end_td_html_file(fp);
  write_start_td_html_file(fp,0);
  fprintf(fp,sdata2);
  write_end_td_html_file(fp);
  write_end_tr_html_file(fp);
}

void 
write_header_table_string2(FILE *fp, const char *svalue, const char *sdata1, 
			   const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_start_th_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_th_html_file(fp);
  write_start_th_html_file(fp,0);
  fprintf(fp,sdata1);
  write_end_th_html_file(fp);
  write_start_th_html_file(fp,0);
  fprintf(fp,sdata2);
  write_end_th_html_file(fp);
  write_end_tr_html_file(fp);
}

void 
write_header_table_string3(FILE *fp, const char *svalue, const char *sdata1, 
			   const char *sdata2,const char *sdata3)
{
  write_start_tr_html_file(fp);
  write_th_string(fp,svalue);
  write_th_string(fp,sdata1);
  write_th_string(fp,sdata2);
  write_th_string(fp,sdata3);
  write_end_tr_html_file(fp);
}

void 
write_header_table_string4(FILE *fp, const char *svalue, const char *sdata1, 
			   const char *sdata2,const char *sdata3,
			   const char *sdata4)
{
  write_start_tr_html_file(fp);
  write_th_string(fp,svalue);
  write_th_string(fp,sdata1);
  write_th_string(fp,sdata2);
  write_th_string(fp,sdata3);
  write_th_string(fp,sdata4);
  write_end_tr_html_file(fp);
}

void
write_th_string(FILE *fp, const char *svalue)
{
  write_start_th_html_file(fp,0);
  fprintf(fp,svalue);
  write_end_th_html_file(fp);
}

void 
write_header_table_string5(FILE *fp, const char *svalue, const char *sdata1, 
			   const char *sdata2, const char *sdata3,
			   const char *sdata4,const char *sdata5)
{
  write_start_tr_html_file(fp);
  write_th_string(fp,svalue);
  write_th_string(fp,sdata1);
  write_th_string(fp,sdata2);
  write_th_string(fp,sdata3);
  write_th_string(fp,sdata4);
  write_th_string(fp,sdata5);
  write_end_tr_html_file(fp);
}

void 
write_header_table_string6(FILE *fp, const char *svalue, const char *sdata1, 
			   const char *sdata2, const char *sdata3,
			   const char *sdata4,const char *sdata5,
			   const char *sdata6)
{
  write_start_tr_html_file(fp);
  write_th_string(fp,svalue);
  write_th_string(fp,sdata1);
  write_th_string(fp,sdata2);
  write_th_string(fp,sdata3);
  write_th_string(fp,sdata4);
  write_th_string(fp,sdata5);
  write_th_string(fp,sdata6);
  write_end_tr_html_file(fp);
}

void 
write_header_table_string7(FILE *fp, const char *svalue, const char *sdata1, 
			   const char *sdata2, const char *sdata3,
			   const char *sdata4,const char *sdata5,
			   const char *sdata6,const char *sdata7)
{
  write_start_tr_html_file(fp);
  write_th_string(fp,svalue);
  write_th_string(fp,sdata1);
  write_th_string(fp,sdata2);
  write_th_string(fp,sdata3);
  write_th_string(fp,sdata4);
  write_th_string(fp,sdata5);
  write_th_string(fp,sdata6);
  write_th_string(fp,sdata7);
  write_end_tr_html_file(fp);
}

void 
write_header_table_string8(FILE *fp, const char *svalue, const char *sdata1, 
			   const char *sdata2, const char *sdata3,
			   const char *sdata4,const char *sdata5,
			   const char *sdata6,const char *sdata7,
			   const char *sdata8)
{
  write_start_tr_html_file(fp);
  write_th_string(fp,svalue);
  write_th_string(fp,sdata1);
  write_th_string(fp,sdata2);
  write_th_string(fp,sdata3);
  write_th_string(fp,sdata4);
  write_th_string(fp,sdata5);
  write_th_string(fp,sdata6);
  write_th_string(fp,sdata7);
  write_th_string(fp,sdata8);
  write_end_tr_html_file(fp);
}

void 
write_header_table_string(FILE *fp, const char *svalue, const char *sdata1) 
{
  write_start_tr_html_file(fp);
  write_th_string(fp,svalue);
  write_th_string(fp,sdata1);
  write_end_tr_html_file(fp);
}

void
write_row_num_link_title_string2(FILE *fp,const char *snum, const char *slink,
				 const char *stitle, const char *sdata1, 
				 const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  if(stitle) {
      fprintf(fp,"%s",stitle);
      write_break_html_file(fp);
  }
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp); 
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}


void
write_row_num_link_title_string3(FILE *fp,const char *snum, const char *slink,
				 const char *stitle, const char *sdata1, 
				 const char *sdata2,const char *sdata3)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  if(stitle) {
      fprintf(fp,"%s",stitle);
      write_break_html_file(fp);
  }
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_end_tr_html_file(fp);
}


void
write_row_num_link_title_string3_color(FILE *fp,const char *snum, 
				       const char *slink,
				      const char *stitle, const char *sdata1, 
				       const char *sdata2,const char *sdata3,
				       char *scolor)
{
  write_start_tr_html_file_color(fp,scolor);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  if(stitle) {
      fprintf(fp,"%s",stitle);
      write_break_html_file(fp);
  }
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_end_tr_html_file(fp);
}


void
write_row_num_link_title_string7(FILE *fp,const char *snum, const char *slink,
				 const char *stitle, const char *sdata1, 
				 const char *sdata2,const char *sdata3,
				 const char *sdata4,const char *sdata5,
				 const char *sdata6,const char *sdata7)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  if(stitle) {
      fprintf(fp,"%s",stitle);
      write_break_html_file(fp);
  }
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3); 
  write_td_string(fp,sdata4);	
  write_td_string(fp,sdata5); 
  write_td_string(fp,sdata6);	
  write_td_string(fp,sdata7); 
  write_end_tr_html_file(fp);
}

void
write_row_num_link_title_string7_color(FILE *fp,const char *snum, 
				       const char *slink,
				 const char *stitle, const char *sdata1, 
				 const char *sdata2,const char *sdata3,
				 const char *sdata4,const char *sdata5,
				 const char *sdata6,const char *sdata7,
				       char *scolor)
{
  write_start_tr_html_file_color(fp,scolor);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  if(stitle) {
      fprintf(fp,"%s",stitle);
      write_break_html_file(fp);
  }
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3); 
  write_td_string(fp,sdata4);	
  write_td_string(fp,sdata5); 
  write_td_string(fp,sdata6);	
  write_td_string(fp,sdata7); 
  write_end_tr_html_file(fp);
}

void
write_row_link_title_string2(FILE *fp,const char *slink,const char *stitle,
		       const char *sdata1, const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  if(stitle) {
      fprintf(fp,"%s",stitle);
      write_break_html_file(fp);
  }
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}

void
write_row_num_link_string2(FILE *fp,const char *snum, const char *slink,
			   const char *sdata1, const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}

void
write_row_num_string2(FILE *fp,const char *snum,const char *sdata1, 
		      const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}

void
write_row_num_string3(FILE *fp,const char *snum,const char *sdata1, 
		      const char *sdata2,const char *sdata3)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_end_tr_html_file(fp);
}

void
write_row_num_string4(FILE *fp,const char *snum,const char *sdata1, 
		      const char *sdata2,const char *sdata3,
		      const char *sdata4)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_td_string(fp,sdata4);	
  write_end_tr_html_file(fp);
}

void
write_row_num_string5(FILE *fp,const char *snum,const char *sdata1, 
		      const char *sdata2,const char *sdata3,
		      const char *sdata4,const char *sdata5)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_td_string(fp,sdata4);	
  write_td_string(fp,sdata5);	
  write_end_tr_html_file(fp);
}


void
write_row_num_string6(FILE *fp,const char *snum,const char *sdata1, 
		      const char *sdata2,const char *sdata3,
		      const char *sdata4,const char *sdata5,
		      const char *sdata6)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_td_string(fp,sdata4);	
  write_td_string(fp,sdata5);	
  write_td_string(fp,sdata6);	
  write_end_tr_html_file(fp);
}

void
write_row_num_string7(FILE *fp,const char *snum,const char *sdata1, 
		      const char *sdata2,const char *sdata3,
		      const char *sdata4,const char *sdata5,
		      const char *sdata6,const char *sdata7)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_td_string(fp,sdata4);	
  write_td_string(fp,sdata5);	
  write_td_string(fp,sdata6);	
  write_td_string(fp,sdata7);	
  write_end_tr_html_file(fp);
}


void
write_row_num_link_string3(FILE *fp,const char *snum, const char *slink,
			   const char *sdata1, const char *sdata2,
			   const char *sdata3)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_end_tr_html_file(fp);
}

void
write_row_num_link_string3_color(FILE *fp,const char *snum, const char *slink,
			   const char *sdata1, const char *sdata2,
			   const char *sdata3,char *scolor)
{
  write_start_tr_html_file_color(fp,scolor);
  write_td_string(fp,snum); 
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_end_tr_html_file(fp);
}

void
write_row_num_link_string4(FILE *fp,const char *snum, const char *slink,
			   const char *sdata1, const char *sdata2,
			   const char *sdata3,const char *sdata4)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_link(fp,slink);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_td_string(fp,sdata4);	
  write_end_tr_html_file(fp);
}

void
write_row_link_string2(FILE *fp,const char *slink,const char *sdata1, 
		       const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_start_td_html_file(fp,0);
  write_start_a_href_html_file(fp,slink);
  fprintf(fp,"%s",slink);
  write_end_a_href_html_file(fp);
  write_end_td_html_file(fp);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}

void
write_td_link(FILE *fp,const char *tlink)
{
  char *pl = tlink ? strstr(tlink,"http://") : NULL;
  write_start_td_html_file(fp,0);
  if(tlink && pl)
  {
    write_start_a_href_html_file(fp,tlink);
    fprintf(fp,"%s",tlink);
    write_end_a_href_html_file(fp);
  }
  else if(tlink)
    fprintf(fp,"%s",tlink);

  write_end_td_html_file(fp);
}

void
write_td_link_page(FILE *fp,const char *tlink, const char *tvalue)
{
  char *pl = tlink ? strstr(tlink,"http://") : NULL;
  write_start_td_html_file(fp,0);
  if(tlink && pl)
  {
    write_start_a_href_html_file(fp,tlink);
    if(tvalue)
    {
      fprintf(fp,"%s",tvalue);
      write_break_html_file(fp);
    }
    fprintf(fp,"%s",tlink);
    write_end_a_href_html_file(fp);
  }
  else if(tlink)
    fprintf(fp,"%s",tlink);

  write_end_td_html_file(fp);
}

void
write_td_link_page_wrap(FILE *fp,const char *tlink, const char *tvalue)
{
  char *pl = tlink ? strchr(tlink,'?') : NULL;
  char *pl1 = NULL;
  char *pl2 = NULL;
  char *pl3 = NULL;
  char *pl4 = NULL;
  char *hl = tlink ? strstr(tlink,"http://") : NULL;
  
  if(pl)
  {
      GRStrAllocCopy(pl1,tlink);
      pl = strchr(pl1,'?');
      pl++;
      GRStrAllocCopy(pl2,pl);
      *pl='\0';
      if(pl2 && strlen(pl2) > 50)
      {
	  pl = pl2+50;
	  GRStrAllocCopy(pl3,pl);
	  *pl ='\0';
      }
      if(pl3 && strlen(pl3) > 50)
      {
	  pl = pl3+50;
	  GRStrAllocCopy(pl4,pl);
	  *pl ='\0';
      }
      if(pl4 && strlen(pl4) > 50)
      {
	  *(pl4+50)='\0';
      }
  }
  write_start_td_html_file(fp,0);
  if(tlink && hl)
  {
    write_start_a_href_html_file(fp,tlink);
    if(tvalue)
    {
      fprintf(fp,"%s",tvalue);
      write_break_html_file(fp);
    }
    if(!pl1)
    {
	fprintf(fp,"%s",tlink);
    }
    else
    {
	fprintf(fp,"%s",pl1);
	if(pl2)
	{
	    write_break_html_file(fp);
	    fprintf(fp,"%s",pl2);
	}
	if(pl3)
	{
	    write_break_html_file(fp);
	    fprintf(fp,"%s",pl3);
	}
	if(pl4)
	{
	    write_break_html_file(fp);
	    fprintf(fp,"%s",pl4);
	}
    }
    write_end_a_href_html_file(fp);
  }
  else if(tlink)
      fprintf(fp,"%s",tlink);

  write_end_td_html_file(fp);

  if(pl1) free(pl1); 
  if(pl2) free(pl2); 
  if(pl3) free(pl3); 
  if(pl4) free(pl4); 
}

void
write_verify_http_link(FILE *fp,const char *link,const char *value)
{
  char *hl = link ? strstr(link,"http://") : NULL;
  if(link && hl)
  {
    write_start_a_href_html_file(fp,link);
    if(value)
    {
      fprintf(fp,"%s",value);
      write_break_html_file(fp);
    }		
    fprintf(fp,"%s",link);
    write_end_a_href_html_file(fp);
  } 
  else if(link)
    fprintf(fp,"%s",link);
}

void
write_td_hyperlink(FILE *fp,const char *slink,const char *svalue, 
			    const char *tlink,const char *tvalue, 
			    const char *caption)
{
  write_start_td_html_file(fp,0);
  write_bold_html_file(fp,"Referer :");
  write_verify_http_link(fp,slink,svalue);
  if(caption)
  {
      write_break_html_file(fp);
      write_bold_html_file(fp,"Caption :");
      write_string_html_file(fp,caption);
  }
  if(tlink)
  {
    write_break_html_file(fp);
    write_bold_html_file(fp,"Target :");
    write_verify_http_link(fp,tlink,tvalue);
    write_end_td_html_file(fp);
  }
}

void
write_row_hyperlink_string2(FILE *fp,const char *slink,const char *svalue, 
			    const char *tlink,const char *tvalue, 
			    const char *caption, const char *sdata1, 
			    const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_td_hyperlink(fp,slink,svalue, tlink,tvalue,caption);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}


void
write_row_num_hyperlink_string2(FILE *fp,const char *snum, 
				const char *slink,const char *svalue, 
			    const char *tlink,const char *tvalue, 
			    const char *caption, const char *sdata1, 
			    const char *sdata2)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_hyperlink(fp,slink,svalue, tlink,tvalue,caption);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}

void
write_row_num_hyperlink_string2_color(FILE *fp,const char *snum, 
				const char *slink,const char *svalue, 
			    const char *tlink,const char *tvalue, 
			    const char *caption, const char *sdata1, 
			    const char *sdata2,char *scolor)
{
  write_start_tr_html_file_color(fp,scolor);
  write_td_string(fp,snum); 
  write_td_hyperlink(fp,slink,svalue, tlink,tvalue,caption);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_end_tr_html_file(fp);
}


void
write_row_num_hyperlink_string3(FILE *fp,const char *snum, 
				const char *slink,const char *svalue, 
			    const char *tlink,const char *tvalue, 
			    const char *caption, const char *sdata1, 
			    const char *sdata2, const char *sdata3)
{
  write_start_tr_html_file(fp);
  write_td_string(fp,snum); 
  write_td_hyperlink(fp,slink,svalue, tlink,tvalue,caption);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_end_tr_html_file(fp);
}


void
write_row_num_hyperlink_string3_color(FILE *fp,const char *snum, 
				const char *slink,const char *svalue, 
			    const char *tlink,const char *tvalue, 
			    const char *caption, const char *sdata1, 
			    const char *sdata2, const char *sdata3,
				      char *scolor)
{
  write_start_tr_html_file_color(fp,scolor);
  write_td_string(fp,snum); 
  write_td_hyperlink(fp,slink,svalue, tlink,tvalue,caption);
  write_td_string(fp,sdata1); 
  write_td_string(fp,sdata2);	
  write_td_string(fp,sdata3);	
  write_end_tr_html_file(fp);
}

void
write_row_span_num_hyperlink_string(FILE *fp,const char *snum, 
				const char *slink,const char *svalue, 
			    const char *tlink,const char *tvalue, 
				    const char *caption, const char *sdata1,
				    int span)

{
  write_start_tr_html_file(fp);
  if(snum)
      write_td_string_span_rows_top(fp,(char *)snum,span); 
  write_td_hyperlink(fp,slink,svalue, tlink,tvalue,caption);
  if(sdata1)
      write_td_string_span_rows_top(fp,(char *)sdata1,span); 
  write_end_tr_html_file(fp);
}



void
write_row_span_num_hyperlink_string_color(FILE *fp,const char *snum, 
				const char *slink,const char *svalue, 
			        const char *tlink,const char *tvalue, 
				const char *caption, const char *sdata1,
				int span,char *scolor)

{
  write_start_tr_html_file_color(fp,scolor);
  if(snum)
      write_td_string_span_rows_top(fp,(char *)snum,span); 
  write_td_hyperlink(fp,slink,svalue,tlink,tvalue,caption);
  if(sdata1)
      write_td_string_span_rows_top(fp,(char *)sdata1,span); 
  write_end_tr_html_file(fp);
}


void
write_row_user_graph_table(FILE *fp,const char *snum, const char *suser,
			   const char *rlink, 
			   const char *elink, const char *evalue, 
			   const char *slink,const char *svalue, 
			   const char *tlink,const char *tvalue, 
			   const char *caption, const char *sdata1,
			   int span,char *scolor)

{
  write_start_tr_html_file_color(fp,scolor);
  if(snum)
      write_td_string_span_rows_top(fp,(char *)snum,span); 
  if(suser)
  {
      if(suser[0]!='\0')
	  write_td_string(fp,(char *)suser); 
      else
	  write_td_string_span_rows_top(fp,(char *)"&nbsp;",span); 
  }
  if(rlink)
  {
      if(rlink[0]!='\0')
	  write_td_link_page_wrap(fp,rlink,NULL); 
      else
	  write_td_string_span_rows_top(fp,(char *)"&nbsp;",span); 
  }
  if(elink)
  {
      if(elink[0]!='\0')
	  write_td_link_page(fp,elink,evalue); 
      else
	  write_td_string_span_rows_top(fp,(char *)"&nbsp;",span); 
  }
  if(slink && tlink)
  {     
    if(slink[0]!='\0' && tlink[0]!=0)
	write_td_hyperlink(fp,slink,svalue,tlink,tvalue,caption);
    else
	write_td_string_span_rows_top(fp,(char *)"&nbsp;",span); 
  }
  if(sdata1)
      write_td_string_span_rows_top(fp,(char *)sdata1,span); 
  write_end_tr_html_file(fp);
}





